﻿using UnityEngine;
using System.Collections;
using System.IO;
using LitJson;
using System.Collections.Generic;




public class Datas : MonoBehaviour
{

    public static Datas _instance;
    public string path;
    //public string path = "/users.txt";//相对路径
    private string fullPath;//原始数据的读取路径  完整路径
    private string persistent;//永久路径
    private StreamReader sr;//读取流
    private string temp;//临时Json串
    //private playerInfo[] infos;//玩家信息
    private List<playerInfo> infos = new List<playerInfo>();//玩家信息
    private string name;//保存玩家用户名
    private string passWord;//保存玩家密码
    //目的封装数据格式信息
    public class playerInfo
    {
        public string name;
        public string passWord;
        public string nickName;
    }

    void Awake()
    {
        _instance = this;
        DontDestroyOnLoad(this);
        //        //streamingAssets路径前需要加上文件标记
        //#if UNITY_EDITOR
        //        fullPath = "file://" + Application.streamingAssetsPath + path;
        //#elif UNITY_ANDROID
        //    fullPath=Application.streamingAssetsPath+path;
        //#endif
        //        persistent = Application.persistentDataPath + path;
        //        print(persistent);
        //        //如果文件不存在就复制数据文件到沙盒路径，如果存在就不再复制进行覆盖，这样就保存了游戏数据

        //if (!File.Exists(persistent))
        //    removeData2();
        // StartCoroutine(RemoveData());
        persistent = Application.persistentDataPath + path;
        if (!File.Exists(persistent))
            File.Create(persistent);
            print(persistent);

    }
    void Start()
    {
        string aa = File.ReadAllText(persistent, System.Text.Encoding.UTF8);
        if (string.IsNullOrEmpty(aa))
        {
            return;
        }
        infos = JsonMapper.ToObject<List<playerInfo>>(aa);
    }
    //void removeData2()
    //{
    //Resources.Load("users");
    //}
    //从streamingAssets到沙盒路径
    //IEnumerator RemoveData()
    //{
    //    WWW www = new WWW(fullPath);
    //    yield return www;
    //    if (www.error == null)
    //    {
    //        string text = www.text;
    //        StreamWriter sw = new StreamWriter(persistent);
    //        sw.Write(text);
    //        sw.Close();
    //        sw.Dispose();
    //        print("数据转移成功");
    //    }
    //    else
    //    {
    //        print(www.error);
    //    }

    //}
    //抽取的读取操作
    //public void ReadData()
    //{
    //    sr = new StreamReader(persistent);
    //    temp = sr.ReadToEnd();
    //    sr.Close();
    //    infos = JsonMapper.ToObject<playerInfo[]>(temp);
    //}
    //抽取的写信息的操作
    public void WriteData()
    {//通过对象得到一个Json串
        string json = JsonMapper.ToJson(infos);
        StreamWriter sw = new StreamWriter(persistent);
        sw.Write(json);
        sw.Close();
    }
    //登录逻辑
    public bool LogIn(string name, string pass)
    {
        if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(pass))
        {//如果密码和名字一致，读取一下数据文件得到数据对象
            //ReadData();
            //便利数据对象查找有么有与登录数据一致的对象
            foreach (playerInfo item in infos)
            {//如果登录的名字，密码和一个对象一致
                if (item.name == name && item.passWord == pass)
                {
                    if (item.nickName == "-1")
                    {//检查是否有昵称，没有就登录人物选择场景
                        ChangeSceneManager._instance.ChangeScene(2);
                        //Application.LoadLevel(2);
                    }
                    else
                    {//有就登录主场景
                        ChangeSceneManager._instance.ChangeScene(3);
                    }
                    this.name = name;
                    this.passWord = pass;
                    return true;
                }
            }
        }
        return false;
    }
    //查找有无相同姓名的用户
    public bool FindName(string name)
    {
        //ReadData();
        for (int i = 0; i < infos.Count; i++)
        {
            if (infos[i].name == name)
            {
                return true;
            }

        }
        return false;
    }
    //注册方法
    public bool Register(string name, string pass, string passRepeat)
    {
        if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(pass) && !string.IsNullOrEmpty(passRepeat))
        {

            if (pass == passRepeat)
            {

                if (FindName(name))
                {

                    return false;
                }
                else
                {
                    //ReadData();
                    //for (int i = 0; i < infos.Count; i++)
                    //{
                    //if (infos[i].name == "-1")
                    //{
                    //    infos[i].name = name;
                    //infos[i].passWord = pass;
                    playerInfo bb = new playerInfo();
                    bb.name = name;
                    bb.passWord = pass;
                    bb.nickName = "-1";
                    infos.Add(bb);
                    WriteData();
                    return true;
                    //}
                    //}
                }
            }
        }
        return false;
    }
    //更改信息
    public void ChangeData(string nickName)
    {
        //ReadData();
        for (int i = 0; i < infos.Count; i++)
        {
            if (infos[i].name == name && infos[i].passWord == passWord)
            {
                infos[i].nickName = nickName;
                WriteData();
                return;
            }
        }
    }
    //提取信息
    public string GetNickName()
    {
        //ReadData();
        for (int i = 0; i < infos.Count; i++)
        {
            if (infos[i].name == name && infos[i].passWord == passWord)
            {
                return infos[i].nickName;
            }
        }
        return null;
    }

}
